package cate.game.role.farm.udgpalace;

import cate.common.table.farm.udgpalace.row.UdgPalaceTaskRow;
import cate.common.util.GameResult;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.farm.udgpalace.msg.UdgPalaceTaskRewardResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

@NoteClass("任务")
public class UdgPalaceTask extends RoleDependent {
    @NoteField("击杀守卫数量")
    public int killNum;

    @NoteField("任务奖励领取记录")
    public Map<Integer, Boolean> taskRewardGot;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (taskRewardGot == null) {
            taskRewardGot = new HashMap<>();
        }
    }

    public void reset() {
        killNum = 0;
        taskRewardGot.clear();
    }

    public GameResult<Void> getReward(int tid) {
        GameResult<Void> r = new GameResult<>();
        if (taskRewardGot.containsKey(tid)) {
            return r.fail("奖励已领取");
        }
        UdgPalaceTaskRow taskRow = role.getGame().table.udgPalace.task.get(tid);
        if (taskRow == null) {
            return r.fail("奖励不存在");
        }
        if (killNum < taskRow.num) {
            return r.fail("未达到奖励领取条件");
        }
        taskRewardGot.put(tid, true);
        new MixResAdder()
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .setRes(taskRow.rewardStr)
                .exe(role);
        role.sendNow(new UdgPalaceTaskRewardResp(tid));
        return r;
    }

    public void afterChallenge(boolean win) {
        if (win) {
            killNum++;
        }
    }
}
